/*
Copyright 2009 Jeff Carnes

This file is part of Fluant.

Fluant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Fluant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.amiasleep.fluant.flaPublisher;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class PublishJSFLIO {
	
	
	public static void write(File fla, File publishProfile, Writer writer, boolean closeFLA) throws TemplateOutputException
	{
		try
		{
			Configuration cfg = new Configuration();
			cfg.setClassForTemplateLoading(PublishJSFLIO.class, "/");
			Template template = cfg.getTemplate("publish.ftl");
			Map<String,Object> root = new HashMap<String,Object>();
			
			URL flaUrl = fla.toURL();
			String flaPath = flaUrl.toString().replace("file:/", "file:///");
			root.put("flaPath", flaPath);
			
			URL profileUrl = publishProfile.toURL();
			String profilePath = profileUrl.toString().replace("file:/", "file:///");
			root.put("profilePath", profilePath);
			
			root.put("closeFLA", Boolean.toString(closeFLA));
			template.process(root, writer);
			
			// Wrap this in a try block to make sure we close the writer if there's an IOException
			try
			{
				writer.flush();
			}
			finally
			{
				writer.close();
			}	
		}
		catch(IOException iox)
		{
			throw new TemplateOutputException(iox);
		}
		catch(TemplateException tex)
		{
			throw new TemplateOutputException(tex);
		}
	}
	
	
	
	public static void writeExportJSFL(File fla, File exportedProfile, Writer writer) throws TemplateOutputException
	{
		try
		{
			Configuration cfg = new Configuration();
			cfg.setClassForTemplateLoading(PublishJSFLIO.class, "/");
			Template template = cfg.getTemplate("exportProfile.ftl");
			Map<String,Object> root = new HashMap<String,Object>();
			
			URL flaUrl = fla.toURL();
			String flaPath = flaUrl.toString().replace("file:/", "file:///");
			root.put("flaPath", flaPath);
			
			URL profileUrl = exportedProfile.toURL();
			String profilePath = profileUrl.toString().replace("file:/", "file:///");
			root.put("profilePath", profilePath);
			
			template.process(root, writer);
			
			// Wrap this in a try block to make sure we close the writer if there's an IOException
			try
			{
				writer.flush();
			}
			finally
			{
				writer.close();
			}
		}
		catch(IOException iox)
		{
			throw new TemplateOutputException(iox);
		}
		catch(TemplateException tex)
		{
			throw new TemplateOutputException(tex);
		}
	}

}
